//
//  HWVipColorfulNickNameCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/13.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipColorfulNickNameCell: UITableViewCell {
    var bgView:UIView!
    var tagImageView:UIImageView!
    var headImageView:UIImageView!
    var headProtraitFrame:UIImageView!
    var nameLabel:GranibleLabel!
    var vipTagImageView:UIImageView!
    var contentLabel:UILabel!
    var receiveButton:UIButton!
    var vipTips:UILabel!
    
    var model:HWHeadPortraitFrameModel? {
        didSet {
//            headProtraitFrame.sd_setImage(with: URL(string: model?.frame_img ?? ""), completed: nil)
            headImageView.sd_setImage(with: URL(string: model?.head_img ?? ""), completed: nil)
            nameLabel.text = model?.name
            contentLabel.text = model?.content
            vipTips.text = model?.title
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#000000"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#000000")]
            }
            
            if model?.is_peidai == 0 {
//                receiveButton.setTitle("穿戴", for: .normal)
//                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#4F3200"), for: .normal)
//                receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
                bgView.layer.shadowColor = UIColor.clear.cgColor
                bgView.layer.borderColor = UIColor.clear.cgColor
                
            }else if model?.is_peidai == 1 {
//                receiveButton.setTitle("脱下", for: .normal)
//                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#4F3200"), for: .normal)
//                receiveButton.setBackgroundImage(UIImage(named: "takeOffButton"), for: .normal)
                bgView.layer.shadowColor = UIColor.colorWithHexString(hexString: "#E8EAF1").cgColor
                bgView.layer.shadowOffset = CGSize(width:0,height:2)
                bgView.layer.shadowOpacity = 1
                bgView.layer.shadowRadius = 4
                bgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#E7BD60").cgColor
                bgView.layer.borderWidth = 1
                vipTips.text = "已穿戴"
            }else if model?.is_peidai == 2 {
//                receiveButton.setTitle("领取", for: .normal)
//                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#666666"), for: .normal)
//                receiveButton.setBackgroundImage(UIImage(named: "receiveButtonGray"), for: .normal)
                bgView.layer.shadowColor = UIColor.clear.cgColor
                bgView.layer.borderColor = UIColor.clear.cgColor
            }
            
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWVipColorfulNickNameCell) {
        let reUse:String = "HWVipColorfulNickNameCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWVipColorfulNickNameCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWVipColorfulNickNameCell
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
//        let gradientLayer = CAGradientLayer()
//        gradientLayer.colors = [UIColor.red.cgColor,UIColor.yellow.cgColor]
////        CGPoint(x: 0, y: 0) CGPoint(x: 1, y: 0)
//        //设置渲染的起始结束位置（横向渐变）
//        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
//        gradientLayer.endPoint = CGPoint(x: 0, y: 1)
//
//        //设置其CAGradientLayer对象的frame，并插入view的layer
//        gradientLayer.frame = nameLabel.bounds
//        nameLabel.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        bgView = UIView()
        bgView.backgroundColor = UIColor.colorWithHexString(hexString: "#F3F4F8")
        bgView.layer.cornerRadius = 16*widthScale
        contentView.addSubview(bgView)
//        bgView.layer.shadowColor = UIColor.colorWithHexString(hexString: "#E8EAF1").cgColor
//        bgView.layer.shadowOffset = CGSize(width:0,height:2)
//        bgView.layer.shadowOpacity = 1
//        bgView.layer.shadowRadius = 4
        
//        tagImageView = UIImageView()
//        tagImageView.backgroundColor = .red
//        contentView.addSubview(tagImageView)
        
        headImageView = UIImageView()
        headImageView.backgroundColor = .clear
        headImageView.layer.cornerRadius = 22*widthScale
        headImageView.layer.masksToBounds = true
        bgView.addSubview(headImageView)
        
//        headProtraitFrame = UIImageView()
//        headProtraitFrame.backgroundColor = .clear
//        bgView.addSubview(headProtraitFrame)
        
        nameLabel = GranibleLabel()
        nameLabel.text = "热情桑巴"
        nameLabel.font = .boldSystemFont(ofSize: 12*widthScale)
        nameLabel.colors = [UIColor.colorWithHexString(hexString: "#023047"),UIColor.colorWithHexString(hexString: "#219EBC"),UIColor.colorWithHexString(hexString: "#8ECAE6"),UIColor.colorWithHexString(hexString: "#FFB703"),UIColor.colorWithHexString(hexString: "#FB8500")]
        bgView.addSubview(nameLabel)
        
        
        
        vipTagImageView = UIImageView()
        vipTagImageView.image = UIImage(named: "vipTagImageView")
        bgView.addSubview(vipTagImageView)
        
        contentLabel = HWLabel.createLabel(title: "今天又是元气满满的一天哦~~", color: "#666666", font: 10*widthScale)
        bgView.addSubview(contentLabel)
        
//        receiveButton = UIButton(type: .custom)
//        receiveButton.setTitle("穿戴", for: .normal)
//        receiveButton.titleLabel?.font = .boldSystemFont(ofSize: 12*widthScale)
//        receiveButton.setTitleColor(.colorWithHexString(hexString: "#4F3200"), for: .normal)
//        receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
//        bgView.addSubview(receiveButton)
        
        vipTips = HWLabel.createLabel(title: "VIP1 可领取", color: "#999999", font: 14*widthScale)
        bgView.addSubview(vipTips)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.top.equalTo(15*widthScale)
            make.left.equalTo(21*widthScale)
            make.right.equalTo(-9*widthScale)
            make.bottom.equalTo(0)
        }
        
//        tagImageView.snp.makeConstraints { make in
//            make.left.equalTo(8*widthScale)
//            make.top.equalTo(10*widthScale)
//            make.width.equalTo(57*widthScale)
//            make.height.equalTo(31*widthScale)
//        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(16*widthScale)
            make.width.height.equalTo(44*widthScale)
            make.top.equalTo(13*widthScale)
            make.bottom.equalTo(-13*widthScale)
        }
        
//        headProtraitFrame.snp.makeConstraints { make in
//            make.left.equalTo(15*widthScale)
//            make.top.equalTo(5*widthScale)
//            make.bottom.equalTo(-5*widthScale)
//            make.width.equalTo(54*widthScale)
//            make.height.equalTo(60*widthScale)
//        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(14*widthScale)
            make.top.equalTo(18*widthScale)
        }
        
        vipTagImageView.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp.right).offset(4*widthScale)
            make.width.equalTo(25*widthScale)
            make.height.equalTo(20*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
        }
        
//        receiveButton.snp.makeConstraints { make in
//            make.right.equalTo(-14*widthScale)
//            make.top.equalTo(13*widthScale)
//            make.width.equalTo(56*widthScale)
//            make.height.equalTo(27*widthScale)
//        }
        
        vipTips.snp.makeConstraints { make in
//            make.top.equalTo(receiveButton.snp.bottom).offset(5*widthScale)
//            make.centerX.equalTo(receiveButton)
            make.right.equalTo(-14*widthScale)
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
